package com.grablist.feihu.grablist.pay;

import android.app.Activity;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import com.grablist.feihu.grablist.base.BaseBiz;
import com.grablist.feihu.grablist.comm.MyApplication;
import com.grablist.feihu.grablist.ui.actviity.user.biz.RechargeBiz;
import com.grablist.feihu.grablist.util.LogUtils;

/**
 * Created by Administrator on 2017/11/2.
 */

public class WXPayManger implements IWXPayManger {

    public static final String APP_ID = "wx4d9bda40c1a42a42";
    private IWXAPI api;

    private IPayManager.PayCallback payCallback;

    private Activity activity;

    public WXPayManger(Activity activity) {
        this.activity = activity;
    }

    RechargeBiz rechargeBiz=new RechargeBiz();;


    public void payWX(final Activity activity,String appid,String key,String noncestr,String packagesign,String partnerid,String prepayid,String sign,String timestamp){
        api = WXAPIFactory.createWXAPI(activity, APP_ID, true);
        api.registerApp(APP_ID);
        PayReq req = new PayReq();
        req.appId = appid;
        req.partnerId = partnerid;
        req.prepayId =prepayid;
        req.packageValue =packagesign;
        req.nonceStr = noncestr;
        req.timeStamp = timestamp;
        req.sign = sign;
        api.sendReq(req);
    }


    @Override
    public void pay(String rechargeSettingId, String payMethod, PayCallback payCallback) {
        rechargeBiz.recharge(MyApplication.userInfo.getUserCode(),rechargeSettingId,payMethod,new BaseBiz.Callback<String>() {
            @Override
            public void onSuccess(String o) {
//                try {
//                    String appid =((JSONObject)jsonObject).getString(JsonKey.WXAPPID);
//                    String key =((JSONObject)jsonObject).getString(JsonKey.KEY);
//                    String noncestr =((JSONObject)jsonObject).getString(JsonKey.NONCESTR);
//                    String packagesign =((JSONObject)jsonObject).getString(JsonKey.PACKAGE);
//                    String partnerid =((JSONObject)jsonObject).getString(JsonKey.PARTNERID);
//                    String prepayid =((JSONObject)jsonObject).getString(JsonKey.PREPAYID);
//                    String sign =((JSONObject)jsonObject).getString(JsonKey.SIGN);
//                    String timestamp =((JSONObject)jsonObject).getString(JsonKey.TIMESTAMP);
//                    payWX(activity,appid,key,noncestr,packagesign,partnerid,prepayid,sign,timestamp);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
            }

            @Override
            public void onFailure(String o) {
                LogUtils.d("2err:"+o);
            }
        });

    }
}
